<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests\CompanyQuestionsPostRequest;
use App\Models\Company;
use Illuminate\Support\Facades\Response;
use App\Models\Questions;
use Illuminate\Support\Facades\DB;

class QuestionsController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        if(!$this->user->can('viewExam')) abort(404);
        
        // 搜索处理
        $post = $request->all();
        if (!empty($post)) {
            /*$infos = $info->where('name', 'LIKE', '%'.$post['company_name'].'%')
                          ->where('auth_id', $post['auth_id'])
                          ->where(function($query) use ($post) {
                                $query->where('auth_at', '>=', $post['auth_at_start'])
                                      ->where('auth_at', '<=', $post['auth_at_end']);
                            })
                            ->paginate(20);*/
        } else {
            // $infos = $info->paginate(20);
        }

        $questions = Questions::paginate(20);
        return view('admin.enterprise.questions.index')
                ->with('questions', $questions);
                /*->with('auths',$auths)*/;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        if(!$this->user->can('editExam')) abort(404);
        
        $companys = Company::all();
        return view('admin.enterprise.questions.edit')->with('blademod', 'add')
                    ->with('companys', $companys);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(CompanyQuestionsPostRequest $request)
    {
        if(!$this->user->can('editExam')) 
            return $this->formatJson('error',$this->permerror);
        
        $company_id = $request->get('questions_id', 0);
        $post = array_except($request->all(), ['_token', 'questions_id']);
        $post['answer'] = strtoupper(trim($post['answer']));
        if($company_id > 0 AND !empty($post)) {
            $company_quest = Questions::find($company_id);
            foreach ($post as $key => $val) {
                $company_quest->$key = $val;
            }
            $company_quest->save();
        } else
            Questions::firstOrCreate($post);
        return Response::json(array('id'=>$company_id,'status'=>'success','msg'=>'保存数据成功！','redirectUrl'=>'/admin/enterprise/questions'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        if(!$this->user->can('viewExam')) abort(404);
        
        $companys = Company::all();
        $question = Questions::find($id);
        return view('admin.enterprise.questions.edit')->with('question', $question)
                    ->with('companys', $companys);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($question_id)
    {
        if(!$this->user->can('deleteExam')) 
            return $this->formatJson('error',$this->permerror);
        
        $quest = Questions::find($question_id);
        
        $inExam = DB::table('examination_question')->where('question_id','=',$question_id)->get();
        if(!empty($inExam))
            return $this->formatJson('error','已有试卷关联到该试题，若想删除，请先删除关联！');
        
        if ($quest->delete())
            return  Response::json(array('status'=>'success','msg'=>'题目 ['. str_limit($quest->title, 15) . '] 删除成功!'));
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }

    
}
